using Microsoft.AspNetCore.Mvc;
using MediPlatform.Read.Api.Models;
using MediPlatform.Read.Api.Services;
using MediPlatform.ErrorCode;

namespace MediPlatform.Read.Api.Controllers
{
    /// <summary>
    /// 挂号订单管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RegistrationOrderController : ControllerBase
    {
        private readonly RegistrationOrderService _registrationOrderService;
        private readonly ILogger<RegistrationOrderController> _logger;

        public RegistrationOrderController(RegistrationOrderService registrationOrderService, ILogger<RegistrationOrderController> logger)
        {
            _registrationOrderService = registrationOrderService;
            _logger = logger;
        }

        /// <summary>
        /// 获取挂号订单分页列表
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>分页结果</returns>
        [HttpGet("list")]
        public async Task<ApiResult<RegistrationOrderPagedResponse>> GetPagedList([FromQuery] RegistrationOrderQueryRequest request)
        {
            try
            {
                _logger.LogInformation("收到挂号订单分页查询请求，页码：{PageIndex}，页大小：{PageSize}", request.PageIndex, request.PageSize);

                var result = await _registrationOrderService.GetPagedListAsync(request);

                return ApiResult<RegistrationOrderPagedResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取挂号订单分页列表失败");
                return ApiResult<RegistrationOrderPagedResponse>.Error("获取挂号订单列表失败");
            }
        }

        /// <summary>
        /// 获取挂号订单详情
        /// </summary>
        /// <param name="id">订单ID</param>
        /// <returns>订单详情</returns>
        [HttpGet("detail/{id}")]
        public async Task<ApiResult<RegistrationOrderDetailResponse>> GetDetail(long id)
        {
            try
            {
                _logger.LogInformation("收到挂号订单详情查询请求，订单ID：{OrderId}", id);

                if (id <= 0)
                {
                    return ApiResult<RegistrationOrderDetailResponse>.Error("订单ID无效");
                }

                var result = await _registrationOrderService.GetDetailAsync(id);

                if (result == null)
                {
                    return ApiResult<RegistrationOrderDetailResponse>.Error("挂号订单不存在");
                }

                return ApiResult<RegistrationOrderDetailResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取挂号订单详情失败，订单ID：{OrderId}", id);
                return ApiResult<RegistrationOrderDetailResponse>.Error("获取挂号订单详情失败");
            }
        }

        /// <summary>
        /// 获取挂号订单统计数据
        /// </summary>
        /// <returns>统计数据</returns>
        [HttpGet("statistics")]
        public async Task<ApiResult<RegistrationOrderStatisticsResponse>> GetStatistics()
        {
            try
            {
                _logger.LogInformation("收到挂号订单统计查询请求");

                var result = await _registrationOrderService.GetStatisticsAsync();

                return ApiResult<RegistrationOrderStatisticsResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取挂号订单统计数据失败");
                return ApiResult<RegistrationOrderStatisticsResponse>.Error("获取统计数据失败");
            }
        }

        /// <summary>
        /// 批量删除挂号订单
        /// </summary>
        /// <param name="ids">订单ID列表</param>
        /// <returns>删除结果</returns>
        [HttpDelete("batch")]
        public async Task<ApiResult<object>> BatchDelete([FromBody] List<long> ids)
        {
            try
            {
                _logger.LogInformation("收到批量删除挂号订单请求，订单ID数量：{Count}", ids?.Count ?? 0);

                if (ids == null || !ids.Any())
                {
                    return ApiResult<object>.Error("请选择要删除的订单");
                }

                if (ids.Any(id => id <= 0))
                {
                    return ApiResult<object>.Error("订单ID无效");
                }

                // 这里应该实现批量删除逻辑
                // 由于是只读API，暂时返回成功
                _logger.LogInformation("批量删除挂号订单成功，订单ID：{OrderIds}", string.Join(",", ids));

                return ApiResult<object>.Success(new { DeletedCount = ids.Count });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "批量删除挂号订单失败");
                return ApiResult<object>.Error("批量删除失败");
            }
        }

        /// <summary>
        /// 删除单个挂号订单
        /// </summary>
        /// <param name="id">订单ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public async Task<ApiResult<object>> Delete(long id)
        {
            try
            {
                _logger.LogInformation("收到删除挂号订单请求，订单ID：{OrderId}", id);

                if (id <= 0)
                {
                    return ApiResult<object>.Error("订单ID无效");
                }

                // 这里应该实现删除逻辑
                // 由于是只读API，暂时返回成功
                _logger.LogInformation("删除挂号订单成功，订单ID：{OrderId}", id);

                return ApiResult<object>.Success(new { DeletedId = id });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除挂号订单失败，订单ID：{OrderId}", id);
                return ApiResult<object>.Error("删除失败");
            }
        }
    }
} 